package com.cat.dataStructure03;

import java.io.*;
import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://www.luogu.com.cn/problem/P1967
 * @create 2025/11/20 16:34
 * @since JDK17
 */

public class Solution94 {
    static int MAXN = 10001, MAXM = 50001, LIMIT = 17, n, m, q, cnt, power;
    static int[][] edge = new int[MAXM << 1][3];
    static int[] to = new int[MAXM << 1], head = new int[MAXN], next = new int[MAXM << 1], val = new int[MAXM << 1], f = new int[MAXN];
    static int[] deep = new int[MAXN];
    static int[][] stjump = new int[MAXN][LIMIT], stmin = new int[MAXN][LIMIT];
    static boolean[] vis = new boolean[MAXN];
    static int log2(int n) {
        int ans = 0;
        while ((1 << ans) <= (n >> 1)) {
            ans++;
        }
        return ans;
    }

    static void build() {
        cnt = 1;
        power = log2(n);
        Arrays.fill(head, 1, n + 1, 0);
        Arrays.fill(vis, 1, n + 1, false);
        for (int i = 1; i <= n; i++) {
            f[i] = i;
        }
    }
    static int find(int x) {
        if (f[x] != x) {
            f[x] = find(f[x]);
        }
        return f[x];
    }
    static void add(int u, int v, int w) {
        next[cnt] = head[u];
        to[cnt] = v;
        val[cnt] = w;
        head[u] = cnt++;
    }
    static void kruskal() { // 建图
        Arrays.sort(edge, 0, m, (a, b) -> (b[2] - a[2]));
        for (int i = 0; i < m; i++) {
            var e = edge[i];
            int a = e[0], b = e[1], c = e[2];
            int fa = find(a), fb = find(b);
            if (fa != fb) {
                add(a, b, c);
                add(b, a, c);
                f[fa] = fb;
            }
        }
    }
    static void dfs(int u, int w, int f) {
        vis[u] = true;
        if (f == 0) {
            deep[u] = 1;
            stjump[u][0] = u;
            stmin[u][0] = Integer.MAX_VALUE;
        } else {
//            System.out.println(f);
            deep[u] = deep[f] + 1;
            stjump[u][0] = f;
            stmin[u][0] = w;
        }
        for (int p = 1; p <= power; p++) {
            stjump[u][p] = stjump[stjump[u][p - 1]][p - 1];
            stmin[u][p] = Math.min(stmin[u][p - 1], stmin[stjump[u][p - 1]][p - 1]);
        }
        for (int e = head[u]; e != 0; e = next[e]) {
            if (!vis[to[e]]) {
                dfs(to[e], val[e], u);
            }
        }
    }
    static int lca(int x, int y) {
        if (find(x) != find(y)) {
            return -1;
        }
        if (deep[x] < deep[y]) {
            int temp = x;
            x = y;
            y = temp;
        }
        int ans = Integer.MAX_VALUE;
        for (int p = power; p >= 0; p--) {
            if (deep[stjump[x][p]] >= deep[y]) {
                ans = Math.min(ans, stmin[x][p]);
                x = stjump[x][p];
            }
        }
        if (x == y) {
            return ans;
        }
        for (int p = power; p >= 0; p--) {
            if (stjump[x][p] != stjump[y][p]) {
                ans = Math.min(ans, Math.min(stmin[x][p], stmin[y][p]));
                x = stjump[x][p];
                y = stjump[y][p];
            }
        }
        ans = Math.min(ans, Math.min(stmin[x][0], stmin[y][0]));
        return ans;
    }


    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StreamTokenizer in = new StreamTokenizer(br);
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        in.nextToken();
        n = ((int) in.nval);
        in.nextToken();
        m = ((int) in.nval);
        for (int i = 0; i < m; i++) {
            in.nextToken();
            edge[i][0] = ((int) in.nval);
            in.nextToken();
            edge[i][1] = ((int) in.nval);
            in.nextToken();
            edge[i][2] = ((int) in.nval);
        }
        build();
        kruskal();
        for (int i = 1; i <= n; i++) {
            if (!vis[i]) {
                dfs(i, 0, 0);
            }
        }
        in.nextToken();
        q = ((int) in.nval);
        for (int i = 0, x, y; i < q; i++) {
            in.nextToken();
            x = ((int) in.nval);
            in.nextToken();
            y = ((int) in.nval);
            out.println(lca(x, y));
        }

        out.flush();
        out.close();
        br.close();
    }
}
